﻿using ServiceStack;
/// <summary>
/// 格式化返回的时间格式
/// </summary>
public class DatetimeJsonConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
    private readonly string format;
    public DatetimeJsonConverter(string _format)
    {
        format = _format;
    }
    public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
        //return DateTime.Parse(reader.GetString() ?? string.Empty);
        if (reader.TokenType == JsonTokenType.String)
        {
            if (reader.TryGetDateTime(out DateTime datetime))
                return datetime;
        }
        return new DateTime();
    }

    public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
    {
        writer.WriteStringValue(value.ToString(format));
    }
}

